<?php
namespace support;

use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Webman\Config;

class Log
{
    protected static $_instances = [];

    public static function channel($name = 'default')
    {
        if (isset(static::$_instances[$name])) {
            return static::$_instances[$name];
        }

        $config = Config::get("log.$name");
        if (!$config) {
            return null;
        }

        $logger = new Logger($name);
        foreach ($config['handlers'] as $handler_config) {
            $handler = new $handler_config['class'](...($handler_config['constructor'] ?? []));
            if (isset($handler_config['formatter'])) {
                $formatter = new $handler_config['formatter']['class'](...($handler_config['formatter']['constructor'] ?? []));
                $handler->setFormatter($formatter);
            }
            $logger->pushHandler($handler);
        }

        return static::$_instances[$name] = $logger;
    }
    
    public static function __callStatic($name, $arguments)
    {
        return static::channel('default')->{$name}(...$arguments);
    }
} 